package cm.util.chart;


import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

public class PieChart {
	public static JFreeChart createChart(String title,PieDataset data){
		JFreeChart chart=ChartFactory.createPieChart3D(title, data, true, false, false);
		chart.setTitle(new TextTitle(title,new Font("宋体",Font.BOLD,15)));
		PiePlot3D plot=(PiePlot3D)chart.getPlot();
		plot.setLabelGenerator(new StandardPieSectionLabelGenerator( 
				"{0}={1}({2})", NumberFormat.getNumberInstance(), 
				new DecimalFormat("0.00%"))); 
		plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator( 
		"{0}={1}({2})",NumberFormat.getNumberInstance(), 
		new DecimalFormat("0.00%"))); 
		plot.setLabelFont(new Font("宋体",Font.BOLD,10));
		plot.setBackgroundPaint(Color.WHITE);
		plot.setBackgroundImageAlpha(0.65f);
		plot.setForegroundAlpha(0.65f); 
		plot.setCircular(false, false); 

		LegendTitle legend=chart.getLegend();
		legend.setItemFont(new Font("宋体",Font.BOLD,10));
		return chart;
	}
	public static DefaultPieDataset getDataset()
	{
		DefaultPieDataset dataset=new DefaultPieDataset();
		dataset.setValue("技术人员", 25);
		dataset.setValue("后勤人员", 12);
		dataset.setValue("销售人员", 26);
		
		return dataset;
	}
	public static void main(String[] args) 
	{
		try
		{
			OutputStream os=new FileOutputStream("company.jpeg");
			ChartUtilities.writeChartAsJPEG(os,createChart("人员",getDataset()),400,300);
			os.close();
		}
		catch(Exception e)
		{
			System.out.print(e.toString());
		}
		ChartFrame chartFrame=new ChartFrame("企业人员条形图",createChart("人员",getDataset()));
		chartFrame.pack();
		chartFrame.setVisible(true);
	}
}
